package server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPServer {
	/**
	 * @param args
	 * @throws SocketException 
	 */
	public static void main(String[] args) throws IOException {
		String request;							// Message received by the server.
		String reply = "PONG";					// Message the server will send back to the client.
		byte[] msgOut = (reply).getBytes();		// Message to send in the packet.
		final int serverPort = 5555;			// Server port.
		
		// Waiting for a request.
		DatagramSocket s = new DatagramSocket(serverPort);
		byte[] msg = new byte[256];
		DatagramPacket p = new DatagramPacket(msg, 256);
		System.out.println("Waiting for a request...\n");
		s.receive(p);
		request = new String(p.getData(), 0, p.getLength());
		System.out.println("Request received: " + request + "\n");

		// Replying to a request.
		DatagramPacket pr = new DatagramPacket(msgOut, msgOut.length, p.getAddress(), p.getPort());
		s.send(pr);
		System.out.println("Reply sent: " + reply + "\n");			
	}
}
